package GUI;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import CommonGui.LengthFilter;
import DataObjects.Bon;
import GUI.PfUpdateTableOrder.BonPanel;
import Messages.MessageCheckExistsEmpForClockInOut;
import Messages.MessageCheckExistsEmpForClockInOutReply;


public class CheckPermission extends JFrame {
	
	private int x;
	private int y;
	private Dimension screenSize;
	private MainMenu mainMenu;
	private JPanel statusPanel;
	
	private PfUpdateTableOrder pfUpdateTableOrder;
	private BonPanel bonPanel;
	private Vector<Bon> orders;
	private Container container;
	private Bon oneRow;
	private CheckPermission checkPermission;
	
	
	public CheckPermission (MainMenu mm,PfUpdateTableOrder pfUpdate, BonPanel bonPanel, Vector<Bon> orders, Container container, Bon oneRow)
	{
		super();
		this.checkPermission=this;
		this.mainMenu=mm;
		this.pfUpdateTableOrder=pfUpdate;
		this.bonPanel=bonPanel;
		this.orders=orders;
		this.container=container;
		this.oneRow=oneRow;
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		x=screenSize.width;
		y=screenSize.height;
		
		this.setPreferredSize(new Dimension(x/2, y/2));
		//this.setBounds(x/2, y/2, 10*x/20, 10*y/20);
		
		this.setVisible(true);
		this.pack();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
		initialize();
		
		
	}

	
	private void initialize()
	{
		JPanel panel=new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		
		VKBDigitsOnly vkbPanel=new VKBDigitsOnly(new Color(200,250,230));
		panel.add(getTextPanel());
		panel.add(vkbPanel);	
		this.add(panel);
		
		
	}
	
	
	private JPanel getTextPanel()
	{
		JPanel textPanel=new JPanel ();
		textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.X_AXIS));
		JLabel textLabel=new JLabel("      קוד מנהל: ");
		textLabel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
		textLabel.setForeground(Color.blue);
		
		JTextField textField=new JTextField();
		textField.setFont(new Font("Tahoma", Font.BOLD , 19));
		AbstractDocument doc = (AbstractDocument) textField.getDocument();
		doc.setDocumentFilter(new LengthFilter(doc.getLength(), 4));
		textField.setHorizontalAlignment(JTextField.CENTER);
		textField.setPreferredSize(new Dimension(x/50,y/20));
		textLabel.setLabelFor(textField);
		textPanel.add(getStatusPanel());
		textPanel.add(textField);
		textPanel.add(textLabel);
	
		
		
		textField.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void changedUpdate(DocumentEvent arg0) {
				
				
			}

			@Override
			public void insertUpdate(DocumentEvent arg0) {
				Document temp=arg0.getDocument();
				int size=temp.getLength();
				String currText=null;
				int num=1;
				
				try {
					currText=temp.getText(0, size);
				} catch (BadLocationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				try {
					 num=Integer.parseInt(currText);
				  } catch (NumberFormatException e) {
					  JOptionPane.showMessageDialog(null,"מספר לא תקין");
				  }
				if(size!=1)
				{
					statusPanel.removeAll();
					statusPanel.validate();
					statusPanel.repaint();
				}
				if(size==1 && num!=0)         //if the input includes 4 digits we will display the name of the waiter on the screen 
				{
					MessageCheckExistsEmpForClockInOut msgCheck=new MessageCheckExistsEmpForClockInOut(currText);
					try {
						mainMenu.ms.client.sendToServer(msgCheck);
						MessageCheckExistsEmpForClockInOutReply result = (MessageCheckExistsEmpForClockInOutReply)mainMenu.ms.client.GetMessage();
						int permission=result.getUserPermission();
						if(permission<2) 
						{
							statusPanel.removeAll();
							JLabel wrongUserLabel=new JLabel("מספר לא תקין");
							wrongUserLabel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
							wrongUserLabel.setForeground(Color.blue);
							statusPanel.add(wrongUserLabel);
							wrongUserLabel.setVisible(true);
							statusPanel.validate();
							statusPanel.repaint();
						}
						else
						{
							statusPanel.removeAll();
							JLabel correctUserLabel=new JLabel("תקין");
							correctUserLabel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
							correctUserLabel.setForeground(Color.blue);
							statusPanel.add(correctUserLabel);
							correctUserLabel.setVisible(true);
							statusPanel.validate();
							statusPanel.repaint();
							
							 bonPanel.remove(container);
				             orders.remove(oneRow);
				             bonPanel.validate();
				             bonPanel.repaint();
				             checkPermission.dispose();
				             
							
						}
						//String name=result.getUserName();
						//setNameOnTheScreen(name);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
				/*else
				{
					hindNameOnTheScreen();
				}*/
			}

			@Override
			public void removeUpdate(DocumentEvent arg0) {				
				
			}			
		});
		
		return textPanel;
		
	}
	
	
	private JPanel getStatusPanel()
	{
		statusPanel=new JPanel();
		statusPanel.setPreferredSize(new Dimension(200,y/20));
		statusPanel.setMaximumSize(new Dimension(200,y/20));
		statusPanel.setMinimumSize(new Dimension(200,y/20));
		return statusPanel;
	}
	
}
